package com.dg.cuckoo.admin.service.impl;

import com.dg.cuckoo.admin.dto.basicInformation.permissionSettings.JobDto;
import com.dg.cuckoo.admin.dto.basicInformation.permissionSettings.JobFeaturesDto;
import com.dg.cuckoo.admin.dto.other.FeaturesDto;
import com.dg.cuckoo.admin.repository.FeaturesRepository;
import com.dg.cuckoo.admin.repository.JobFeaturesRepository;
import com.dg.cuckoo.admin.repository.JobRepository;
import com.dg.cuckoo.admin.service.JobFeaturesService;
import com.dg.cuckoo.core.ServiceException;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class JobFeaturesServiceimpl implements JobFeaturesService {

    @Autowired
    private FeaturesRepository featuresRepository;
    @Autowired
    private JobRepository jobRepository;
    @Autowired
    private JobFeaturesRepository jobFeaturesRepository;

    @Override
    public String select() {
        List<String> jobs = new ArrayList<>();
        List<String> featuress = new ArrayList<>();

        List<JobDto> jobDtos = jobRepository.findAll();
        for (JobDto j : jobDtos) {
            jobs.add(j.getJob());
        }
        List<FeaturesDto> featuresDtos = featuresRepository.findFeaturesDtosByLevel(1);
        for (FeaturesDto f : featuresDtos) {
            featuress.add(f.getName());
        }

        JSONObject json = new JSONObject();
        json.put("jobs", jobs);                  //岗位名称
        json.put("featuress", featuress);        //功能列表
        return json.toString();
    }

    @Override
    public List<String> openOneLabel(String name) {
        List<String> names = new ArrayList<>();
        List<FeaturesDto> dtos = featuresRepository.findBySuperiorName(name);
        for (FeaturesDto f : dtos) {
            names.add(f.getName());
        }
        return names;
    }

    @Override
    public void save(String jobName, List<String> featuress) {
        if (jobName == null) {
            throw new ServiceException("请输入岗位名称");
        }
        if(featuress.size()>0){
            List<JobFeaturesDto> dtos = jobFeaturesRepository.findByJob(jobName);
            jobFeaturesRepository.deleteAll(dtos);
            for (String s : featuress) {
                JobFeaturesDto dto = new JobFeaturesDto();
                dto.setJob(jobName);
                dto.setFeatures(s);
                jobFeaturesRepository.save(dto);
            }
        }else {
            List<JobFeaturesDto> dtos = jobFeaturesRepository.findByJob(jobName);
            jobFeaturesRepository.deleteAll(dtos);
        }

    }

    @Override
    public List<String> openJobName(String jobName) {
        List<String> data = new ArrayList<>();                //响应的结果
        List<FeaturesDto> features = new ArrayList<>();

        List<JobFeaturesDto> list = jobFeaturesRepository.findByJob(jobName);
        for (JobFeaturesDto j : list) {
            String name = j.getFeatures();
            features.add(featuresRepository.findByName(name));

        }
        for (FeaturesDto f : features) {

            while (true){
                if (f.getSuperiorName() != null) {
                    FeaturesDto byName = featuresRepository.findByName(f.getSuperiorName());
                    f=byName;
                }
                if(f.getSuperiorName()==null){
                    break;
                }
            }
            data.add(f.getName());
        }
        return data;
    }

    @Transactional
    @Override
    public void delect(String id) {
        JobDto jobDto = jobRepository.findById(id).get();
        List<JobFeaturesDto> byJob = jobFeaturesRepository.findByJob(jobDto.getJob());
        jobFeaturesRepository.deleteAll(byJob);
        jobRepository.deleteById(id);
    }
}
